<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是GDB——调试工具的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV02-04-GDB-调试工具">
<meta property="og:url" content="https://sumumm.github.io/post/a638936c.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是GDB——调试工具的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-04-GDB-%E8%B0%83%E8%AF%95%E5%B7%A5%E5%85%B7/img/image-20220513102606547.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-04-GDB-%E8%B0%83%E8%AF%95%E5%B7%A5%E5%85%B7/img/image-20220513102733497.png">
<meta property="article:published_time" content="2022-03-02T09:50:26.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.989Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV02-编译基础">
<meta property="article:tag" content="GCC与GDB">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-04-GDB-%E8%B0%83%E8%AF%95%E5%B7%A5%E5%85%B7/img/image-20220513102606547.png">


<link rel="canonical" href="https://sumumm.github.io/post/a638936c.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/a638936c.html","path":"post/a638936c.html","title":"LV02-04-GDB-调试工具"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV02-04-GDB-调试工具 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E8%B0%83%E8%AF%95%E5%99%A8"><span class="nav-text">一、调试器</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81GDB-%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="nav-text">二、GDB 是什么？</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81GDB%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">三、GDB的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="nav-text">1.基本使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1%E6%96%87%E4%BB%B6%E5%A4%84%E7%90%86"><span class="nav-text">1.1文件处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2%E5%90%AF%E5%8A%A8-GDB-%E8%B0%83%E8%AF%95%E5%99%A8"><span class="nav-text">1.2启动 GDB 调试器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3%E5%85%B3%E9%97%AD%E8%B0%83%E8%AF%95%E5%99%A8"><span class="nav-text">1.3关闭调试器</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%9F%BA%E6%9C%AC%E8%B0%83%E8%AF%95%E5%91%BD%E4%BB%A4"><span class="nav-text">2.基本调试命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-list-%E5%91%BD%E4%BB%A4"><span class="nav-text">2.1 list 命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-run-%E5%91%BD%E4%BB%A4"><span class="nav-text">2.2 run 命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-break-%E5%91%BD%E4%BB%A4"><span class="nav-text">2.3 break 命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-info-%E5%91%BD%E4%BB%A4"><span class="nav-text">2.4 info 命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-delete-%E5%91%BD%E4%BB%A4"><span class="nav-text">2.5 delete 命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-next-%E5%91%BD%E4%BB%A4"><span class="nav-text">2.6 next 命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-7-step-%E5%91%BD%E4%BB%A4"><span class="nav-text">2.7 step 命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-8-until-%E5%91%BD%E4%BB%A4"><span class="nav-text">2.8 until 命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-9-continue-%E5%91%BD%E4%BB%A4"><span class="nav-text">2.9 continue 命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-10-print"><span class="nav-text">2.10 print </span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-10-1%E6%9F%A5%E7%9C%8B%E5%8F%98%E9%87%8F"><span class="nav-text">2.10.1查看变量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-10-2%E4%BF%AE%E6%94%B9%E5%8F%98%E9%87%8F%E5%80%BC"><span class="nav-text">2.10.2修改变量值</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-10-3%E6%9F%A5%E7%9C%8B%E6%95%B0%E7%BB%84"><span class="nav-text">2.10.3查看数组</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-10-4%E6%9F%A5%E7%9C%8B%E4%B8%8D%E5%90%8C%E6%96%87%E4%BB%B6%E5%90%8C%E5%90%8D%E5%8F%98%E9%87%8F"><span class="nav-text">2.10.4查看不同文件同名变量</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%A4%9A%E8%BF%9B%E7%A8%8B%E8%B0%83%E8%AF%95"><span class="nav-text">3.多进程调试</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E8%B0%83%E8%AF%95"><span class="nav-text">4.多线程调试</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81TUI-%E6%A8%A1%E5%BC%8F"><span class="nav-text">四、TUI 模式</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%90%AF%E5%8A%A8%E6%96%B9%E5%BC%8F"><span class="nav-text">1.启动方式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%9B%B8%E5%85%B3%E5%91%BD%E4%BB%A4"><span class="nav-text">2.相关命令</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/a638936c.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV02-04-GDB-调试工具 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV02-04-GDB-调试工具
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-03-02 17:50:26" itemprop="dateCreated datePublished" datetime="2022-03-02T17:50:26+08:00">2022-03-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">LV02-编译基础</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>5k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>18 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是GDB——调试工具的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-04-GDB-%E8%B0%83%E8%AF%95%E5%B7%A5%E5%85%B7/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、调试器"><a href="#一、调试器" class="headerlink" title="一、调试器"></a><font size=3>一、调试器</font></h1><p>程序中的错误主要分为两类，分别为语法错误和逻辑错误：程序中的语法错误几乎都可以由编译器诊断出来，很容易就能发现并解决；逻辑错误指的是代码思路或者设计上的缺陷，程序出现逻辑错误的症状是：代码能够编译通过，没有语法错误，但是运行结果不对。对于这类错误，只能靠我们自己去发现和纠正。</p>
<p>程序中出现的语法错误可以借助编译器解决；但逻辑错误则只能靠自己解决，而解决逻辑错误最高效的方法，就是借助调试工具对程序进行调试。</p>
<p>比如，让代码一行一行运行，而不是一次性全部运行，让程序停在某个地方，然后查看当前变量的值，或者内存中的数据，这样我们不就可以监控程序运行的每个细节了吗，哪里出了问题，也会很快找到。</p>
<p>编译程序需要借助专业的编译器，调试程序也需要借助专业的辅助工具，即调试器。</p>
<details class="folding-tag" blue><summary> 点击查看常见调试器 </summary>
              <div class='content'>
              <table>    <tr><td align="center" width=170px>Remote Debugger</td><td align="left">Remote Debugger 是 VC/VS 自带的调试器，与整个IDE无缝衔接，使用非常方便。</td></tr>    <tr><td align="center" width=170px>WinDbg</td><td align="left">大名鼎鼎的 Windows 下的调试器，它的功能甚至超越了 Remote Debugger，它还有一个命令行版本（cdb.exe），但是这个命令行版本的调试器指令比较复杂。</td></tr>    <tr><td align="center" width=170px>LLDB</td><td align="left">XCode 自带的调试器，Mac OS X 下开发必备调试器。</td></tr>    <tr><td align="center" width=170px>GDB</td><td align="left">Linux下使用最多的一款调试器，也有 Windows 的移植版。</td></tr></table>
              </div>
            </details>



<h1 id="二、GDB-是什么？"><a href="#二、GDB-是什么？" class="headerlink" title="二、GDB 是什么？"></a><font size=3>二、GDB 是什么？</font></h1><p> GDB  全称 GNU symbolic debugger ，诞生于  GNU  计划（同时诞生的还有  GCC 、 Emacs  等），是  Linux  下常用的程序调试器。当下的  GDB  支持调试多种编程语言编写的程序，包括  C 、 C++ 、 Go 、 Objective-C 、 OpenCL 、 Ada  等。我们可以通过  GDB  调试器实现以下几个功能：</p>
<ul>
<li>程序启动时，可以按照我们自定义的要求运行程序，例如设置参数和环境变量；</li>
<li>可使被调试程序在指定代码处暂停运行，并查看当前程序的运行状态（例如当前变量的值，函数的执行结果等），即支持断点调试；</li>
<li>程序执行过程中，可以改变某个变量的值，还可以改变代码的执行顺序，从而尝试修改程序中出现的逻辑错误。</li>
</ul>
<table>
    <tr>
        <td align="center" width=200>GDB官网</td>
        <td align="left"><a href="https://sourceware.org/gdb" target="_blank">https://sourceware.org/gdb  <i class="fa fa-external-link-alt"></i> </a></td>  
    </tr>
</table>




<h1 id="三、GDB的使用"><a href="#三、GDB的使用" class="headerlink" title="三、GDB的使用"></a><font size=3>三、GDB的使用</font></h1><p><strong>【说明】</strong>后边输入的命令中，带有 (gdb) 的表示在gdb调试器中输入的命令。</p>
<h2 id="1-基本使用"><a href="#1-基本使用" class="headerlink" title="1.基本使用"></a><font size=3>1.基本使用</font></h2><h3 id="1-1文件处理"><a href="#1-1文件处理" class="headerlink" title="1.1文件处理"></a><font size=3>1.1文件处理</font></h3><p>在使用 GDB 之前，需要对程序编译进行处理，需要加上 -g 参数才能生成满足 GDB 要求的可执行文件。例如</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gcc -g test.c -o test</span><br></pre></td></tr></table></figure>

<p>这样生成的可执行文件才可以正常使用 GDB 进行调试。</p>
<p><strong>【注意】</strong></p>
<p>（1）只有在代码处于运行或暂停状态时才能查看变量值。</p>
<p>（2）设置断点后程序在指定行之前停止 。</p>
<h3 id="1-2启动-GDB-调试器"><a href="#1-2启动-GDB-调试器" class="headerlink" title="1.2启动 GDB 调试器"></a><font size=3>1.2启动 GDB 调试器</font></h3><p>当生成满足要求的可执行程序后，可以如下命令启动调试器：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gdb &lt;filename&gt; # 这是在shell中执行的命令</span><br></pre></td></tr></table></figure>

<ul>
<li>filename ：生成的可执行程序名称。</li>
</ul>
<p><strong>【注意】</strong> gdb 在执行完一个命令后不输入任何命令直接回车， gdb 会默认执行上一个命令。</p>
<p>输入下边命令后会发现终端输出以下信息：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1</span><br><span class="line">Copyright (C) 2018 Free Software Foundation, Inc.</span><br><span class="line">License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;</span><br><span class="line">This is free software: you are free to change and redistribute it.</span><br><span class="line">There is NO WARRANTY, to the extent permitted by law.  Type &quot;show copying&quot;</span><br><span class="line">and &quot;show warranty&quot; for details.</span><br><span class="line">This GDB was configured as &quot;x86_64-linux-gnu&quot;.</span><br><span class="line">Type &quot;show configuration&quot; for configuration details.</span><br><span class="line">For bug reporting instructions, please see:</span><br><span class="line">&lt;http://www.gnu.org/software/gdb/bugs/&gt;.</span><br><span class="line">Find the GDB manual and other documentation resources online at:</span><br><span class="line">&lt;http://www.gnu.org/software/gdb/documentation/&gt;.</span><br><span class="line">---Type &lt;return&gt; to continue, or q &lt;return&gt; to quit---</span><br></pre></td></tr></table></figure>

<p>这是是一些提示信息，我们按下 Enter  就可以继续进入调试器，而按下 q 按键就可以退出调试器。进入调试器后，命令格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) &lt;command&gt;</span><br></pre></td></tr></table></figure>

<p> (gdb) 是提示符，就像 Shell 中的 # 和 $ 一样，后边跟上命令即可。</p>
<h3 id="1-3关闭调试器"><a href="#1-3关闭调试器" class="headerlink" title="1.3关闭调试器"></a><font size=3>1.3关闭调试器</font></h3><p>我们调试结束了，需要退出调试器的时候我们可以使用如下命令退出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) q</span><br></pre></td></tr></table></figure>

<p>然后按下 Enter 按键就可以退出 gdb 调试了。</p>
<h2 id="2-基本调试命令"><a href="#2-基本调试命令" class="headerlink" title="2.基本调试命令"></a><font size=3>2.基本调试命令</font></h2><h3 id="2-1-list-命令"><a href="#2-1-list-命令" class="headerlink" title="2.1 list 命令"></a><font size=3>2.1 list 命令</font></h3><p>在调试器中可缩写为 l ，可以列出所调试程序的代码。使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">(gdb) l [option]</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">或者</span></span><br><span class="line">(gdb) list [option]</span><br></pre></td></tr></table></figure>

<p><strong>【参数说明】</strong></p>
<table>
    <tr><td align="center" width=100px>option</td><td align="center">说明</td></tr>
    <tr><td align="center" width=100px>省略</td><td align="left">gbd会将gdb当前所处的行以及后面的代码打印在屏幕上。</td></tr>
    <tr><td align="center" width=100px>lineNumber</td><td align="left">打印指定行附近的代码。如list 8，gdb会将8行前后的代码打印在屏幕上。</td></tr>
    <tr><td align="center" width=100px>-</td><td align="left">gbd会将gdb当前所处的行前面的代码打印在屏幕上。</td></tr>
    <tr><td align="center" width=100px>functionName</td><td align="left">打印名称为functionName的函数的上下文的代码。</td></tr>
</table>


<h3 id="2-2-run-命令"><a href="#2-2-run-命令" class="headerlink" title="2.2 run 命令"></a><font size=3>2.2 run 命令</font></h3><p>在调试器中可缩写为 r ，可以开始运行程序，若无断点，则会全部执行一遍，遇到第一个断点会停下。使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">(gdb) r</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">或者</span></span><br><span class="line">(gdb) run</span><br></pre></td></tr></table></figure>

<h3 id="2-3-break-命令"><a href="#2-3-break-命令" class="headerlink" title="2.3 break 命令"></a><font size=3>2.3 break 命令</font></h3><p>在调试器中可缩写为 b ，用于设置断点。使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">(gdb) b [option]</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">或者</span></span><br><span class="line">(gdb) break [option]</span><br></pre></td></tr></table></figure>

<p><strong>【参数说明】</strong></p>
<table>
    <tr><td align="center" width=100px>option</td><td align="center">说明</td></tr>
    <tr><td align="left" width=100px>lineNumber</td><td align="left">在指定的代码行打断点</td></tr>
    <tr><td align="left" width=100px>filename:lineNumber</td><td align="left">gbd会在名称为filename的文件中的第lineNumber行打断点。</td></tr>
    <tr><td align="left" width=100px> filename:function</td><td align="left">gdb在名称为filename的文件中的function函数入口处打断点。</td></tr>
    <tr><td align="left" width=100px> *address</td><td align="left">gdb在在程序运行的内存地址处打断点。</td></tr>
</table>



<h3 id="2-4-info-命令"><a href="#2-4-info-命令" class="headerlink" title="2.4 info 命令"></a><font size=3>2.4 info 命令</font></h3><p>用于查看断点信息，使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">(gdb) info b     # 查看所有断点信息</span><br><span class="line">(gdb) info b n   # 查看第 n 个断点信息</span><br></pre></td></tr></table></figure>

<p>断点信息显示格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Num     Type           Disp Enb Address            What</span><br><span class="line">1       breakpoint     keep y   0x00005555555546d6 in main at 00GCC.c:6</span><br><span class="line">        breakpoint already hit 1 time</span><br><span class="line">2       breakpoint     keep y   0x00005555555546c8 in main at 00GCC.c:5</span><br><span class="line">3       breakpoint     keep y   0x00005555555546b9 in main at 00GCC.c:3</span><br><span class="line">4       breakpoint     keep y   0x00005555555546b9 in main at 00GCC.c:2</span><br></pre></td></tr></table></figure>

<h3 id="2-5-delete-命令"><a href="#2-5-delete-命令" class="headerlink" title="2.5 delete 命令"></a><font size=3>2.5 delete 命令</font></h3><p>用于删除断点。使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">(gdb) delete  b     # 删除所有断点信息</span><br><span class="line">(gdb) delete  b n   # 删除第 n 个断点信息</span><br></pre></td></tr></table></figure>

<h3 id="2-6-next-命令"><a href="#2-6-next-命令" class="headerlink" title="2.6 next 命令"></a><font size=3>2.6 next 命令</font></h3><p>在调试器中可缩写为 n ，用于单步运行程序，但是<strong>如果有函数调用的话，它不会进入函数体</strong>。使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">(gdb) n [option]</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">或者</span></span><br><span class="line">(gdb) next [option]</span><br></pre></td></tr></table></figure>

<p><strong>【参数说明】</strong></p>
<table>
    <tr><td align="center" width=100px>option</td><td align="center">说明</td></tr>
    <tr><td align="center" width=100px>省略</td><td align="left">一条一条单步执行。</td></tr>
    <tr><td align="center" width=100px>count</td><td align="left">执行后边的count条指令。</td></tr>
</table>


<h3 id="2-7-step-命令"><a href="#2-7-step-命令" class="headerlink" title="2.7 step 命令"></a><font size=3>2.7 step 命令</font></h3><p>在调试器中可缩写为 s ，用于单步运行程序，<strong>如果有函数调用的话，它会进入函数体</strong>。使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">(gdb) s [option]</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">或者</span></span><br><span class="line">(gdb) step [option]</span><br></pre></td></tr></table></figure>

<p><strong>【参数说明】</strong></p>
<table>
    <tr><td align="center" width=100px>option</td><td align="center">说明</td></tr>
    <tr><td align="center" width=100px>省略</td><td align="left">一条一条单步执行。</td></tr>
    <tr><td align="center" width=100px>n</td><td align="left">执行后边的 n 条指令。</td></tr>
</table>




<h3 id="2-8-until-命令"><a href="#2-8-until-命令" class="headerlink" title="2.8 until 命令"></a><font size=3>2.8 until 命令</font></h3><p>在调试器中可缩写为 u ，用于运行到某一行程序或者直接运行完循环，如果有函数调用的话，它会进入函数体。使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">(gdb) u [option]</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">或者</span></span><br><span class="line">(gdb) until [option]</span><br></pre></td></tr></table></figure>

<p><strong>【参数说明】</strong></p>
<table>
    <tr><td align="center" width=100px>option</td><td align="center">说明</td></tr>
    <tr><td align="center" width=100px>省略</td><td align="left">可以使 GDB调试器快速运行完当前的循环体，并运行至循环体外停止。</td></tr>
    <tr><td align="center" width=100px>n</td><td align="left">某行代码的行号，以指示 GDB 调试器直接执行至指定位置后停止。</td></tr>
</table>


<p><strong>【注意】</strong>until 命令并非任何情况下都会发挥这个作用，只有当执行至循环体尾部（最后一行代码）时，until 命令才会发生此作用；反之，until 命令和 next 命令的功能一样，只是单步执行程序。</p>
<h3 id="2-9-continue-命令"><a href="#2-9-continue-命令" class="headerlink" title="2.9 continue 命令"></a><font size=3>2.9 continue 命令</font></h3><p>在调试器中可缩写为 c ，用于继续运行程序。当程序在某一断点处停止后，用该指令可以继续执行，直至遇到断点或者程序结束。使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">(gdb) c</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">或者</span></span><br><span class="line">(gdb) continue</span><br></pre></td></tr></table></figure>

<h3 id="2-10-print"><a href="#2-10-print" class="headerlink" title="2.10 print "></a><font size=3>2.10 print </font></h3><h4 id="2-10-1查看变量"><a href="#2-10-1查看变量" class="headerlink" title="2.10.1查看变量"></a><font size=3>2.10.1查看变量</font></h4><p>在调试器中可缩写为 p ，用于继打印指定变量的值。使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">(gdb) p [option --][/fmt] expr</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">或者</span></span><br><span class="line">(gdb) print [option --][/fmt] expr</span><br></pre></td></tr></table></figure>

<p><strong>【参数说明】</strong></p>
<ul>
<li>option ：表示该命令所支持的选项，这些选项可以控制  print  命令输出指定内容的变量或者表达式的值，是可选的；</li>
</ul>
<details class="folding-tag" blue><summary> 点击查看 option </summary>
              <div class='content'>
              <table>    <tr><td align="center">option</td><td align="center">说明</td></tr>    <tr><td align="center">-address on|off</td><td align="left">查看某一指针变量的值时，是否同时打印其占用的内存地址，默认值为 on。该选项等同于单独执行 set print address on|off 命令。</td></tr>    <tr><td align="center">-array on|off</td><td align="left">是否以便于阅读的格式输出数组中的元素，默认值为 off。该选项等同于单独执行 set printf array on|off 命令。</td></tr>    <tr><td align="center">-array-indexes on|off</td><td align="left">对于非字符类型数组，在打印数组中每个元素值的同时，是否同时显示每个元素对应的数组下标，默认值为 off。该选项等同于单独执行 set print array-indexes on|off 命令。</td></tr>    <tr><td align="center">-pretty on|off</td><td align="left">以便于阅读的格式打印某个结构体变量的值，默认值为 off。该选项等同于单独执行 set print pretty on|off 命令。</td></tr></table>
              </div>
            </details>

<ul>
<li>fmt ：指定输出变量或表达式值时所采用的格式，是可选的；</li>
</ul>
<details class="folding-tag" blue><summary> 点击查看 fmt </summary>
              <div class='content'>
              <table>    <tr><td align="center" width=100px>/fmt</td><td align="center">功 能</td></tr>    <tr><td align="center" width=100px>/x</td><td align="left">以十六进制的形式打印出整数。</td></tr>    <tr><td align="center" width=100px>/d</td><td align="left">以有符号、十进制的形式打印出整数。</td></tr>    <tr><td align="center" width=100px>/u</td><td align="left">以无符号、十进制的形式打印出整数。</td></tr>    <tr><td align="center" width=100px>/o</td><td align="left">以八进制的形式打印出整数。</td></tr>    <tr><td align="center" width=100px>/t</td><td align="left">以二进制的形式打印出整数。</td></tr>    <tr><td align="center" width=100px>/f</td><td align="left">以浮点数的形式打印变量或表达式的值。</td></tr>    <tr><td align="center" width=100px>/c</td><td align="left">以字符形式打印变量或表达式的值。</td></tr></table><p>例如，</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) p /x a  # 以十六进制显示变量a的值</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<ul>
<li>expr ：指定要查看的变量或表达式。</li>
</ul>
<p><strong>【注意】</strong> </p>
<p>（1）options  参数和  &#x2F;fmt  或者  expr  之间，必须用 -- （  2  个  -  字符）分隔。</p>
<p>（2）[ ] 里边的内容是可选的，可以没有。</p>
<h4 id="2-10-2修改变量值"><a href="#2-10-2修改变量值" class="headerlink" title="2.10.2修改变量值"></a><font size=3>2.10.2修改变量值</font></h4><p>print 命令还支持修改变量值，一般格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) print variable=value</span><br></pre></td></tr></table></figure>

<ul>
<li>variable ：表示要修改的变量名。</li>
<li>value ：表示修改后的值。</li>
</ul>
<h4 id="2-10-3查看数组"><a href="#2-10-3查看数组" class="headerlink" title="2.10.3查看数组"></a><font size=3>2.10.3查看数组</font></h4><p>print 命令还支持 @ 运算符，多用于查看数组，一般格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) print first@len</span><br></pre></td></tr></table></figure>

<ul>
<li>first ：用于指定数组查看区域内的首个元素的值。</li>
<li>len ：用于指定自 first 元素开始查看的元素个数。</li>
</ul>
<h4 id="2-10-4查看不同文件同名变量"><a href="#2-10-4查看不同文件同名变量" class="headerlink" title="2.10.4查看不同文件同名变量"></a><font size=3>2.10.4查看不同文件同名变量</font></h4><p>当程序中包含多个作用域不同但名称相同的变量或表达式时，可以借助 :: 运算符明确指定要查看的目标变量或表达式。:: 运算符的语法格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">(gdb) print file::variable</span><br><span class="line">(gdb) print function::variable</span><br></pre></td></tr></table></figure>

<ul>
<li><p>file ：用于指定具体的文件名。</p>
</li>
<li><p>funciton ：用于指定具体所在函数的函数名。</p>
</li>
<li><p>variable ：表示要查看的目标变量或表达式。</p>
</li>
</ul>
<h2 id="3-多进程调试"><a href="#3-多进程调试" class="headerlink" title="3.多进程调试"></a><font size=3>3.多进程调试</font></h2><p>此部分为后边学习多进程编程后补充而来。注意这里的命令我前边都加了 $ ，表示在终端中的标识，下边的 (gdb) 表示在调试器中的命令前缀标识。</p>
<ul>
<li>（1）文件编译处理</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$</span><span class="language-bash">gcc -g test.c -o <span class="built_in">test</span></span></span><br></pre></td></tr></table></figure>

<ul>
<li>（2）启动 GDB</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$</span><span class="language-bash">gdb <span class="built_in">test</span></span></span><br></pre></td></tr></table></figure>

<ul>
<li>（3）开始调试</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) start # 单步运行程序，程序会在第一条功能语句停下来。</span><br></pre></td></tr></table></figure>

<ul>
<li>（4）设置调试子进程或者父进程</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">(gdb) set follow-fork-mode child     # 设置GDB调试子进程</span><br><span class="line">(gdb) set follow-fork-mode parent    # 设置GDB调试父进程</span><br></pre></td></tr></table></figure>

<ul>
<li>（5）设置调试多个进程</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) set detach-on-fork on/off  # 设置GDB跟踪调试单个进程或多个</span><br></pre></td></tr></table></figure>

<p> on  表示只调试父进程或子进程的其中一个，(根据 follow-fork-mode 来决定)，这是默认的模式。off 表示父子进程都在 gdb 的控制之下，其中一个进程正常调试(根据 follow-fork-mode 来决定)，另一个进程会被设置为暂停状态。然后我们可以通过下边的命令显示可以调试的所有进程：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) info inferiors # 显示GDB调试的进程</span><br></pre></td></tr></table></figure>

<p>如下图所示：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-04-GDB-%E8%B0%83%E8%AF%95%E5%B7%A5%E5%85%B7/img/image-20220513102606547.png" alt="image-20220513102606547" style="zoom:50%;" />

<p>随后我们就可以从上边显示的信息中心选择自己需要调试的进程号，进行指定进程的调试：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) inferiors &lt;进程序号1,2,3...&gt;  # 切换GDB调试的进程</span><br></pre></td></tr></table></figure>

<h2 id="4-多线程调试"><a href="#4-多线程调试" class="headerlink" title="4.多线程调试"></a><font size=3>4.多线程调试</font></h2><p>此部分为后边学习多线程编程后补充而来。注意这里的命令我前边都加了 $ ，表示在终端中的标识，下边的 (gdb) 表示在调试器中的命令前缀标识。。</p>
<ul>
<li>（1）文件编译处理</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$</span><span class="language-bash">gcc -g test.c -o <span class="built_in">test</span></span></span><br></pre></td></tr></table></figure>

<ul>
<li>（2）启动 GDB</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$</span><span class="language-bash">gdb <span class="built_in">test</span></span></span><br></pre></td></tr></table></figure>

<ul>
<li>（3）开始调试</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) start # 单步运行程序，程序会在第一条功能语句停下来。</span><br></pre></td></tr></table></figure>

<ul>
<li>（4）显示所有线程</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) info thread</span><br></pre></td></tr></table></figure>

<ul>
<li>（5）切换线程</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) thread &lt;id&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>（6）为指定线程添加断点</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) break location thread &lt;id&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>（7）设置线程锁</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) set scheduler-locking on/off # on 表示其他线程会暂停，可以单独调试一个线程</span><br></pre></td></tr></table></figure>

<h1 id="四、TUI-模式"><a href="#四、TUI-模式" class="headerlink" title="四、TUI 模式"></a><font size=3>四、TUI 模式</font></h1><p>先上一张图吧，简单来说就是可以对照源码进行调试，这就是  GDB  自带的  GDB TUI ，它看起来比直接使用 list 要更美观一点：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-04-GDB-%E8%B0%83%E8%AF%95%E5%B7%A5%E5%85%B7/img/image-20220513102733497.png" alt="image-20220513102733497" style="zoom: 33%;" />

<h2 id="1-启动方式"><a href="#1-启动方式" class="headerlink" title="1.启动方式"></a><font size=3>1.启动方式</font></h2><div class="tabs" id="tabname"><ul class="nav-tabs"><li class="tab active"><a href="#tabname-1">方式一</a></li><li class="tab"><a href="#tabname-2">方式二</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname-1"><ul>
<li>直接使用命令启动</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">gdbtui -q 需要调试的程序名</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">或者</span></span><br><span class="line">gdb -tui -q 需要调试的程序名</span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="tabname-2"><ul>
<li>快捷键</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Ctrl + x + a</span><br></pre></td></tr></table></figure>

<p>正常开启 GDB 调试，然后按快捷键就可以启动啦。</p></div></div></div>



<h2 id="2-相关命令"><a href="#2-相关命令" class="headerlink" title="2.相关命令"></a><font size=3>2.相关命令</font></h2><ul>
<li>（1） layout 命令</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">命令</span></span><br><span class="line">(gdb) layout src  # 显示源代码窗口</span><br><span class="line">(gdb) layout asm  # 显示汇编窗口</span><br><span class="line">(gdb) layout regs # 显示源代码/汇编和寄存器窗口</span><br><span class="line">(gdb) layout split  # 显示源代码和汇编窗口</span><br><span class="line">(gdb) layout next # 显示下一个layout</span><br><span class="line">(gdb) layout prev # 显示上一个layout</span><br><span class="line">(gdb) winheight src + num # 将代码窗口的高度扩大 num 行代码</span><br><span class="line">(gdb) winheight src - num # 将代码窗口的高度降低 num 行代码</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">快捷键</span></span><br><span class="line">Ctrl + L # 刷新窗口</span><br><span class="line">Ctrl + x + 1 # 单窗口模式，显示一个窗口</span><br><span class="line">Ctrl + x + 2 # 双窗口模式，显示两个窗口</span><br><span class="line">Ctrl + x + a # 回到传统模式，即退出layout，回到执行layout之前的调试窗口。</span><br></pre></td></tr></table></figure>

<ul>
<li>（2） focus 命令</li>
</ul>
<p>在默认设置下，方向键和  PageUp&#x2F;PageDown  都是用来控制  GDB TUI  的  src  窗口的，所以我们在终端中常用上下键显示前一条命令和后一条命令的功能就没有了。但是我们可以通过命令将窗口焦点移动到命令窗口，这样就可以啦。我们可以在 GDB 调试中查看 focus 命令的帮助如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">(gdb) help focus</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">然后我们会得到如下提示</span></span><br><span class="line">focus, fs</span><br><span class="line">Set focus to named window or next/prev window.</span><br><span class="line">Usage: focus [WINDOW-NAME | next | prev]</span><br><span class="line">Use &quot;info win&quot; to see the names of the windows currently being di</span><br><span class="line">splayed.</span><br></pre></td></tr></table></figure>

<p>其中的 WINDOW-NAME 可选的有如下几种：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">src  : the source window</span><br><span class="line">asm  : the disassembly window</span><br><span class="line">regs : the register display</span><br><span class="line">cmd  : the command window</span><br></pre></td></tr></table></figure>

<p>当我们执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(gdb) help cmd</span><br></pre></td></tr></table></figure>

<p>这个时候，窗口焦点就在命令行窗口啦，我们就可以正常使用上下键来选择历史命令啦</p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/a638936c.html">LV02-04-GDB-调试工具</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2022年03月02日 - 17:50</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/a638936c.html" title="LV02-04-GDB-调试工具">https://sumumm.github.io/post/a638936c.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> LV02-编译基础</a>
              <a href="/tags/GCC%E4%B8%8EGDB/" rel="tag"><i class="fa fa-tag"></i> GCC与GDB</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/73483bf2.html" rel="prev" title="LV01-14-C语言-预处理">
                  <i class="fa fa-angle-left"></i> LV01-14-C语言-预处理
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/9720d703.html" rel="next" title="LV02-02-shell-07-sed命令的使用">
                  LV02-02-shell-07-sed命令的使用 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
